package com.example.teach.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.teach.entity.Category;
import com.example.teach.entity.Result;
import com.example.teach.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping("/add")
    public Result add(@RequestBody Category category) {
        if(categoryService.exists(new QueryWrapper<>(category))){
            return Result.error("该分类已存在");
        }
        return Result.success(categoryService.save(category));
    }

    @DeleteMapping("/remove")
    public Result remove(@RequestBody Category category) {
        return Result.success(categoryService.removeById(category.getId()));
    }

    @PutMapping("/update")
    public Result update(@RequestBody Category category) {
        return Result.success(categoryService.updateById(category));
    }

    @GetMapping("/list")
    public Result list(){
        return Result.success(categoryService.list());
    }

    @PostMapping("/select")
    public Result selectById(@RequestParam Integer id){
        return categoryService.selectById(id);
    }

}
